const {
  HttpException,
} = require('../core/HttpException')

const errorHandle = async (ctx, next) => {
  try {
    await next();
  } catch (error) {
    if (error instanceof HttpException) {
      ctx.body = {
        message: error.message,
        err_code: error.errCode,
        method: ctx.method,
        url: ctx.path
      };
      ctx.status = error.status;
    } else {
      ctx.body = {
        message: 'we made a mistake (*^▽^*)',
        err_code: 999,
        method: ctx.method,
        url: ctx.path
      }
      ctx.status = 500;
    }
  }
}

module.exports = errorHandle;